<template>
	<div>
		<ContentWrap>
			<!-- 列表 -->
			<el-table v-loading="loading" :data="list" style="margin-top: 20px" stripe>
				<el-table-column align="center" label="ID" prop="id"/>
				<el-table-column align="center" label="对话名称" prop="title"/>
				<el-table-column
						sortable
						align="center"
						label="创建时间"
						prop="createTime"
						:formatter="dateFormatter"
				/>
				<el-table-column :width="300" align="center" label="操作">
					<template #default="scope">
						<el-button
								v-hasPermi="['enterprise:user:update']"
								link
								type="primary"
								@click="aiDetails(scope.row)"
						>
							查看详情
						</el-button>
					</template>
				</el-table-column>
			</el-table>
			<!-- 分页 -->
			<div class="flex align-items" style="justify-content: center">
				<Pagination
						v-model:limit="queryParams.pageSize"
						v-model:page="queryParams.pageNo"
						:total="total"
						@pagination="getList"
				/>
			</div>
		</ContentWrap>
		<el-dialog
				destroy-on-close
				v-model="dialogVisible"
				title="Tips"
				width="90vw"
				top="5vh"
				:before-close="handleClose"
		>
			<aiVue style="height: 90vh"/>
			<template #footer>
				<div class="dialog-footer">
					<el-button @click="dialogVisible = false">Cancel</el-button>
					<el-button type="primary" @click="dialogVisible = false">
						Confirm
					</el-button>
				</div>
			</template>
		</el-dialog>
	</div>
</template>

<script setup lang="ts">
import {dateFormatter} from '@/utils/formatTime'
import {GetOrgList, GetLevelList} from '@/api/system/org'
import {orgUserPage, orgUserDelete, orgUserUpdate} from '@/api/enterprise/user'
import {ChatConversationApi} from "@/api/ai/chat/conversation";
import aiVue from "./ai/chat/index/index.vue"

const dialogVisible = ref(false);
const message = useMessage() // 消息弹窗
const {query} = useRoute()
const {push} = useRouter()

const aiDetails = async (row: object) => {
  sessionStorage.setItem('aiRow', JSON.stringify(row));
  dialogVisible.value = true;
  // push({
  //   path: "/index",
  //   query: {
  //     aiId: row.id,
  //   }
  // })
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

const queryFormRef = ref(null)
const loading = ref(false) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数据
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  org: '',
  orgName: '',
  level: '',
  levelName: '',
  nickname: '',
  nicknameFirst: '',
  status: '',
  createTime: '',
  jobNo: ''
})

const statusChange = async (row) => {
  await orgUserUpdate(row)
  message.success('修改成功')
  getList()
}

const orgList = ref([])
const apiGetOrgList = async () => {
  orgList.value = await GetOrgList()
}

const orgSelectChange = () => {
  queryParams.level = ''
  levelList.value = []
  if (queryParams.org) apiGetLevelList()
}

// 职级列表
const levelList = ref([])
const apiGetLevelList = async () => {
  levelList.value = await GetLevelList(queryParams.org)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm('公司员工手册确定删除此文档？')
    // 发起删除
    await orgUserDelete(id)
    message.success('删除成功')
    // 刷新列表
    await getList()
  } catch {
  }
}

/** 查询角色列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await ChatConversationApi.getChatConversationMyList({...queryParams, userId: query.id})
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

onMounted(() => {
  apiGetOrgList()
  apiGetLevelList()
  getList()
})
onUpdated(() => {
})
onUnmounted(() => {
})
</script>

<style scoped lang="scss"></style>
